package kz.kstu.scantest.business.common.helper;

import kz.kstu.scantest.business.entity.data.*;
import kz.kstu.scantest.business.entity.data.dto.AnalyzeReportFormDataDTO;
import kz.kstu.scantest.business.entity.data.dto.FormDataDTO;
import kz.kstu.scantest.business.entity.data.FieldNameEnum;
import kz.kstu.scantest.business.entity.data.SubjectEnum;
import kz.kstu.scantest.business.entity.data.FormDataError;
import kz.kstu.scantest.business.util.StringUtil;

import java.util.*;

/**
 * @author Filipp_Stankevich
 *         created: 11.09.11 16:16
 */
public class FormDataHelper {

   public final static List<String> AVAILABLE_ANSWERS = Arrays.asList("A", "B", "C", "D", "E", "a", "b", "c", "d", "e", "?", " ");

    private FormDataHelper() {
    }

    public static int getSubjectBallForField(FormData formData, FieldNameEnum fieldNameEnum) {
        SubjectData firstSubject = getSubjectDataByFieldNameEnum(formData, fieldNameEnum);
        return FormDataHelper.getSubjectBall(firstSubject);
    }


    public static int getSubjectBall(SubjectData subjectData) {
        int ball = 0;
        for (AnswerData answerData : subjectData.getAnswers()) {
            if (answerData.isRight()) {
                ball++;
            }
        }
        return ball;
    }

    public static FormDataDTO convertToDTO(FormData formData) {
        FormDataDTO formDataDTO = new FormDataDTO();
        int firstBall = FormDataHelper.getSubjectBallForField(formData, FieldNameEnum.FIRST_SUBJECT);
        int secondBall = FormDataHelper.getSubjectBallForField(formData, FieldNameEnum.SECOND_SUBJECT);
        int thirdBall = FormDataHelper.getSubjectBallForField(formData, FieldNameEnum.THIRD_SUBJECT);
        int fourthBall = FormDataHelper.getSubjectBallForField(formData, FieldNameEnum.FOURTH_SUBJECT);
        int fifthBall = FormDataHelper.getSubjectBallForField(formData, FieldNameEnum.FIFTH_SUBJECT);
        int sumBall = firstBall + secondBall + thirdBall + fourthBall + fifthBall;
        SubjectData fifthSubject = getSubjectDataByFieldNameEnum(formData, FieldNameEnum.FIFTH_SUBJECT);
        SubjectEnum fifthSubjectEnum = fifthSubject.getSubjectEnum();

        formDataDTO.setFirstBall(firstBall);
        formDataDTO.setSecondBall(secondBall);
        formDataDTO.setThirdBall(thirdBall);
        formDataDTO.setFourthBall(fourthBall);
        formDataDTO.setFifthBall(fifthBall);
        formDataDTO.setSumBall(sumBall);
        String name = formData.getSurname() + StringUtil.SPACE + formData.getInitials();
        formDataDTO.setName(name);
        formDataDTO.setVariant(formData.getVariant());
        formDataDTO.setFifthSubject(fifthSubjectEnum);
        return formDataDTO;
    }

    public static int getSumOfBall(FormData formData) {
        int firstBall = FormDataHelper.getSubjectBallForField(formData, FieldNameEnum.FIRST_SUBJECT);
        int secondBall = FormDataHelper.getSubjectBallForField(formData, FieldNameEnum.SECOND_SUBJECT);
        int thirdBall = FormDataHelper.getSubjectBallForField(formData, FieldNameEnum.THIRD_SUBJECT);
        int fourthBall = FormDataHelper.getSubjectBallForField(formData, FieldNameEnum.FOURTH_SUBJECT);
        int fifthBall = FormDataHelper.getSubjectBallForField(formData, FieldNameEnum.FIFTH_SUBJECT);
        return firstBall + secondBall + thirdBall + fourthBall + fifthBall;
    }

    public static AnalyzeReportFormDataDTO convertToAnalyzeDTO(FormData formData) {
        AnalyzeReportFormDataDTO dto = new AnalyzeReportFormDataDTO();
        Collection<SubjectData> subjectDataCollection = formData.getSubjects();
        SubjectData[] subjectDataArray = subjectDataCollection.toArray(new SubjectData[subjectDataCollection.size()]);
        List<SubjectData> subjectDataList = Arrays.asList(subjectDataArray);

        Map<SubjectEnum, Integer> balls = new HashMap<SubjectEnum, Integer>();
        for (SubjectData subjectData : subjectDataList) {
            Integer ball = FormDataHelper.getSubjectBall(subjectData);
            balls.put(subjectData.getSubjectEnum(), ball);
        }
        dto.setSubjectDataList(subjectDataList);
        String name = formData.getSurname() + " " + formData.getInitials();
        dto.setName(name);
        dto.setScanDate(formData.getCreateDate());
        dto.setVariant(formData.getVariant());
        dto.setBalls(balls);
        return dto;
    }

    public static FormDataError createFormDataError(String errorMessage) {
        FormDataError formDataError = new FormDataError();
        formDataError.setDescription(errorMessage);
        return formDataError;
    }

    public static String getFormDataErrorMessage(FormData formData) {
        StringBuilder errorMessages = new StringBuilder();
        Set<FormDataError> errors = formData.getFormDataErrors();
        if (errors != null) {
            for (FormDataError formDataError : errors) {
                errorMessages.append(formDataError.getDescription());
                errorMessages.append(StringUtil.SEMICOLON);
                errorMessages.append(StringUtil.SPACE);
            }
        }
        return errorMessages.toString();
    }

    public static String getFifthSubjectName(FormData formData) {
        SubjectData subjectData = getSubjectDataByFieldNameEnum(formData, FieldNameEnum.FIFTH_SUBJECT);
        SubjectEnum subject = subjectData != null ? subjectData.getSubjectEnum() : null;
        return  subject != null ? subject.getSubjectName() : null;
    }

    public static SubjectData getSubjectDataByFieldNameEnum(FormData formData, FieldNameEnum fieldNameEnum) {
        List<SubjectData> subjects = formData.getSubjects();
        for (SubjectData subjectData : subjects) {
            if (fieldNameEnum == subjectData.getFieldName()) {
                return subjectData;
            }
        }
        return null;
    }
}
